package cn.changein.framework.admin.setting.contactType.controller;

import cn.changein.framework.admin.setting.contactType.wrapper.ContactTypeWrapper;
import cn.changein.framework.base.controller.BaseController;
import cn.changein.framework.base.pojo.page.PageFactory;
import cn.changein.framework.base.pojo.page.PageInfo;
import cn.changein.framework.kernel.model.response.ResponseData;
import cn.changein.framework.model.setting.contactType.entity.ContactType;
import cn.changein.framework.model.setting.contactType.model.params.ContactTypeParam;
import cn.changein.framework.model.setting.contactType.model.result.ContactTypeResult;
import cn.changein.framework.service.setting.contactType.ContactTypeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * 单位类型控制器
 *

 * @Date 2020-10-27 21:06:32
 * @Copyright: companyName
 * @Version 1.0
 * @Since JDK1.8
 */
@Controller
@RequestMapping("/contactType")
public class ContactTypeController extends BaseController {

    private String PREFIX = "/modular/setting/contactType";

    @Autowired
    private ContactTypeService contactTypeService;

    /**
     * 跳转到主页面
     *

     * @Date 2020-10-27
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/contactType.html";
    }

    /**
     * 新增页面
     *

     * @Date 2020-10-27
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/contactType_add.html";
    }

    /**
     * 编辑页面
     *

     * @Date 2020-10-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/contactType_edit.html";
    }

    /**
     * 新增接口
     *

     * @Date 2020-10-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ContactTypeParam contactTypeParam) {
        return this.contactTypeService.add(contactTypeParam);
    }

    /**
     * 编辑接口
     *

     * @Date 2020-10-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ContactTypeParam contactTypeParam) {
        return this.contactTypeService.update(contactTypeParam);
    }

    /**
     * 删除接口
     *

     * @Date 2020-10-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ContactTypeParam contactTypeParam) {
        return this.contactTypeService.delete(contactTypeParam);
    }

    /**
     * 批量删除单位类型
     *
     * @param contactTypeIds
     * @return
     */
    @RequestMapping(value = "/deleteList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData deleteList(String... contactTypeIds) {
        return this.contactTypeService.deleteList(contactTypeIds);
    }

    /**
     * 查看详情接口
     *

     * @Date 2020-10-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ContactTypeParam contactTypeParam) {
        ContactType detail = this.contactTypeService.getById(contactTypeParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *

     * @Date 2020-10-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public PageInfo list(ContactTypeParam contactTypeParam) {
        Page<Map<String, Object>> pageBySpec = this.contactTypeService.findPageBySpec(contactTypeParam);
        Page<Map<String, Object>> wrapper = new ContactTypeWrapper(pageBySpec).wrap();
        return PageFactory.createPageInfo(wrapper);
    }

    /**
     * 客户、供应商、工厂单位类型
     *

     * @Date 2020-10-27
     */
    @ResponseBody
    @RequestMapping("/listContactType")
    public ResponseData listContactType(ContactTypeParam contactTypeParam) {
        List<ContactTypeResult> list = this.contactTypeService.findListBySpec(contactTypeParam);
        list = Optional.ofNullable(list).orElse(new ArrayList<>());
        return ResponseData.success(list);
    }

}


